/**
 * Created by MetaLab_K on 2016/3/26.
 */
angular.module('metalabPartnerApp').controller('ProjectChargeCtrl', ['$rootScope', '$scope', '$stateParams', 'MetaResources', '$state', 'MetaRemind', 'MetaConstants', '$timeout', '$uibModal',
    function ($rootScope, $scope, $stateParams, resource, $state, MetaRemind, constants, $timeout, $uibModal) {

        $scope.processStates = constants.ProcessStates;

        $scope.page = {//数据模板
            currentPage: 0,
            pageNumber: 5,
            total: 0,
            data: []
        };

        $scope.remarkForm = {//单条备忘信息
            tag: '',
            content: '',
            solutionProcessId: 0
        };

        $scope.remarks = [];//备忘信息集合

        $scope.contractForm = {
            contract: '',
            bankAccount: null,
            bank: '',
            starToDemandProvider: 0,
            starToSolutionProcessProvider: 0
        };

        /**
         * @function 显示联系方式
         */
        $scope.getContactNumber = function (solutionProcess) {//进入联系沟通,并激活邀请对接按钮
            resource.process.demandProcessUpdate({
                id: solutionProcess.demandSideProcess.id,
                _embed: 'SolutionProcess'
            }, {
                checkTechnologySideAt: (new Date())
            }, function (data) {
                console.log('联系技术方合伙人：', data);
                $scope.contacted = true;
                solutionProcess.demandSideProcess.checkTechnologySideAt = data.checkTechnologySideAt;
                solutionProcess.state = data.solutionProcess.state;
                // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                $scope.addRemark('联系技术方合伙人', solutionProcess);
                console.log('联系技术方合伙人：', data);
            }, function (err) {
                MetaRemind.error("出错啦", err)
            });
        };
        /**
         * @function 邀请线下对接
         */
        $scope.beforeOfflineMeeting = function (solutionProcess) {//进入邀请线下对接
            resource.process.demandProcessUpdate({
                id: solutionProcess.demandSideProcess.id,
                _embed: 'SolutionProcess'
            }, {
                inviteTechnologySideAt: (new Date())
            }, function (data) {
                console.log('邀请：', data);
                solutionProcess.demandSideProcess.inviteTechnologySideAt = data.inviteTechnologySideAt;
                solutionProcess.state = data.solutionProcess.state;
                // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                $scope.addRemark("我邀请技术方合伙人进行线下对接，愿意支付诚意金", solutionProcess);
                console.log('邀请：', data);
            }, function (err) {
                MetaRemind.error("出错啦", err)
            });
        };
        /**
         * @function 线下对接完成
         */
        $scope.afterOfflineMeeting = function (solutionProcess) {//进入提交合同
            resource.process.demandProcessUpdate({
                id: solutionProcess.demandSideProcess.id,
                _embed: 'SolutionProcess'
            }, {
                agreeWithReimbursementAt: (new Date())
            }, function (data) {
                console.log('线下对接完成：', data);
                solutionProcess.demandSideProcess.agreeWithReimbursementAt = data.agreeWithReimbursementAt;
                solutionProcess.state = data.solutionProcess.state;
                // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                $scope.addRemark('线下对接完成，诚意金已付', solutionProcess);
                console.log('线下对接完成：', data);
            }, function (err) {
                MetaRemind.error("出错啦", err)
            });
        };
        /**
         * @function 确定签约
         */
        $scope.signContract = function (solutionProcess) {//确定签约
            $scope.agreeToSign = true;
            $scope.addRemark('合作达成，准备签约', solutionProcess);
        };
        /**
         * @function 签订合同
         */
        $scope.serviceValue = function (solutionProcess) {//进入签订合同
            $scope.contractForm.msg = null;
            if ($scope.contractForm.starToDemandProvider < 1 || $scope.contractForm.starToSolutionProcessProvider < 1) {
                $scope.contractForm.msg = "请评分！";
                return;
            }
            if (!$scope.contractForm.contract) {
                $scope.contractForm.msg = "请上传合同！";
                return;
            }
            if (isNaN($scope.contractForm.bankAccount)) {
                $scope.contractForm.msg = "请再次确认您的银行卡账户！";
                return;
            }
            // scope.contractForm = {
            //     contract: '',
            //     bankAccount:null,
            //     bank: '',
            //     starToDemandProvider: 0,
            //     starToSolutionProcessProvider: 0
            // };
            $scope.contractForm.signAContractAt = new Date();
            // resource.process.demandProcessUpdate({
            //     id: solutionProcess.demandSideProcess.id,
            //     _embed: 'SolutionProcess'
            // }, $scope.contractForm, function (data) {
            //     console.log('签订合同a:', solutionProcess.state);
            //     solutionProcess.demandSideProcess.contract = data.contract;
            //     solutionProcess.demandSideProcess.signAContractAt = data.signAContractAt;
            //     solutionProcess.demandSideProcess.bankAccount = data.bankAccount;
            //     solutionProcess.demandSideProcess.bank = data.bank;
            //     solutionProcess.demandSideProcess.starToDemandProvider = data.starToDemandProvider;
            //     solutionProcess.demandSideProcess.starToSolutionProcessProvider = data.starToSolutionProcessProvider;
            //     solutionProcess.state = data.solutionProcess.state;
            //     // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
            //     $scope.addRemark('合作达成，准备签约', solutionProcess);
            //     console.log('签订合同b:', solutionProcess.state);
            // }, function (err) {
            //     MetaRemind.error("出错啦", err)
            // });
        };

        /**
         * @function 添加备注
         */
        $scope.addRemark = function (content, solutionProcess) {
            console.log(solutionProcess);
            $scope.remarkForm.tag = Object.keys($scope.processStates)[solutionProcess.state];
            $scope.remarkForm.content = content ? content : $scope.remarkForm.content;
            $scope.remarkForm.solutionProcessId = solutionProcess.id;
            resource.note.add({
                userId: '_self'
            }, $scope.remarkForm, function (data) {
                //MetaRemind.success("添加成功！");
                getRemark(solutionProcess);
                $scope.remarkForm.content = '';
            }, function (err) {
                //MetaRemind.error("添加失败！");
            });
        };
        ///**
        // * @function 删除备注
        // * @param index 索引
        // */
        //$scope.deleteRemark = function (remarkId, solutionProcessId) {
        //    resource.note.delete({
        //        id: remarkId
        //    }, function (data) {
        //        MetaRemind.success("删除成功！");
        //        getRemark(solutionProcessId);
        //    }, function (err) {
        //        MetaRemind.error("删除失败！");
        //    });
        //};
        /**
         * @function 查看进程的备注
         * @param solutionProcessId 进程id
         */
        var getRemark = function (solutionProcess) {
            console.log('2', solutionProcess);
            resource.note.get({
                userId: '_self',
                solutionProcessId: solutionProcess.id,
                _sort: '-updatedAt'
            }, function (data) {
                solutionProcess.remarks = data;
            }, function (err) {
                MetaRemind.error("出错啦！", err);
            });
        };

        /**
         * @function 拒绝方案
         */
        $scope.reject = function (solutionProcess) {
            var modalInstance = $uibModal.open({
                animation: false,
                templateUrl: 'views/project/common/projectRejectModal.html',
                controller: ['$scope', '$uibModalInstance', function ($scope, $uibModalInstance) {
                    $scope.reason = '';
                    console.log($scope);
                    $scope.cancel = function () {
                        $uibModalInstance.dismiss('cancel');
                    };
                    $scope.ok = function () {
                        $uibModalInstance.close($scope.reason);
                    };
                }],
                size: 'md'
            });
            modalInstance.result.then(function (str) {
                console.log('before reject', str);
                console.log("before reject state:", solutionProcess.state);
                resource.process.demandProcessUpdate({
                    id: solutionProcess.demandSideProcess.id,
                    _embed: 'SolutionProcess'
                }, {
                    causeForCancel: str
                }, function (data) {
                    console.log("rejectdata:", data);
                    // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                    // solutionProcess.state = data.solutionProcess.state;
                    solutionProcess.canceled = data.solutionProcess.canceled;
                    console.log("after reject state:", solutionProcess.state);
                }, function (err) {
                    MetaRemind.error("出错啦", err)
                });
            }, function () {
            });
        };

        /**
         * @function 始终有一个打开
         * @param $event 点击事件
         * @param scope 当前作用域
         */
        $scope.metaToggleOpen = function ($event, scope) {
            if (!$event.target.parentNode.parentNode.parentNode.parentNode.parentNode.classList.contains('panel-open')) {
                scope.toggleOpen();
            }
        };

        $scope.page.data = $scope.$parent.datas.charge;
    }]);
/**
 * Created by MetaLab_K on 2016/3/26.
 */
angular.module('metalabPartnerApp').controller('ProjectChargeCtrl', ['$rootScope', '$scope', '$stateParams', 'MetaResources', '$state', 'MetaRemind', 'MetaConstants', '$timeout', '$uibModal',
    function ($rootScope, $scope, $stateParams, resource, $state, MetaRemind, constants, $timeout, $uibModal) {

        $scope.processStates = constants.ProcessStates;

        $scope.page = {//数据模板
            currentPage: 0,
            pageNumber: 5,
            total: 0,
            data: []
        };

        $scope.remarkForm = {//单条备忘信息
            tag: '',
            content: '',
            solutionProcessId: 0
        };

        $scope.remarks = [];//备忘信息集合

        $scope.contractForm = {
            contract: '',
            bankAccount: null,
            bank: '',
            starToDemandProvider: 0,
            starToSolutionProcessProvider: 0
        };

        /**
         * @function 显示联系方式
         */
        $scope.getContactNumber = function (solutionProcess) {//进入联系沟通,并激活邀请对接按钮
            resource.process.demandProcessUpdate({
                id: solutionProcess.demandSideProcess.id,
                _embed: 'SolutionProcess'
            }, {
                checkTechnologySideAt: (new Date())
            }, function (data) {
                console.log('联系技术方合伙人：', data);
                $scope.contacted = true;
                solutionProcess.demandSideProcess.checkTechnologySideAt = data.checkTechnologySideAt;
                solutionProcess.state = data.solutionProcess.state;
                // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                $scope.addRemark('联系技术方合伙人', solutionProcess);
                console.log('联系技术方合伙人：', data);
            }, function (err) {
                MetaRemind.error("出错啦", err)
            });
        };
        /**
         * @function 邀请线下对接
         */
        $scope.beforeOfflineMeeting = function (solutionProcess) {//进入邀请线下对接
            resource.process.demandProcessUpdate({
                id: solutionProcess.demandSideProcess.id,
                _embed: 'SolutionProcess'
            }, {
                inviteTechnologySideAt: (new Date())
            }, function (data) {
                console.log('邀请：', data);
                solutionProcess.demandSideProcess.inviteTechnologySideAt = data.inviteTechnologySideAt;
                solutionProcess.state = data.solutionProcess.state;
                // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                $scope.addRemark("我邀请技术方合伙人进行线下对接，愿意支付诚意金", solutionProcess);
                console.log('邀请：', data);
            }, function (err) {
                MetaRemind.error("出错啦", err)
            });
        };
        /**
         * @function 线下对接完成
         */
        $scope.afterOfflineMeeting = function (solutionProcess) {//进入提交合同
            resource.process.demandProcessUpdate({
                id: solutionProcess.demandSideProcess.id,
                _embed: 'SolutionProcess'
            }, {
                agreeWithReimbursementAt: (new Date())
            }, function (data) {
                console.log('线下对接完成：', data);
                solutionProcess.demandSideProcess.agreeWithReimbursementAt = data.agreeWithReimbursementAt;
                solutionProcess.state = data.solutionProcess.state;
                // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                $scope.addRemark('线下对接完成，诚意金已付', solutionProcess);
                console.log('线下对接完成：', data);
            }, function (err) {
                MetaRemind.error("出错啦", err)
            });
        };
        /**
         * @function 确定签约
         */
        $scope.signContract = function (solutionProcess) {//确定签约
            $scope.agreeToSign = true;
            $scope.addRemark('合作达成，准备签约', solutionProcess);
        };
        /**
         * @function 签订合同
         */
        $scope.serviceValue = function (solutionProcess) {//进入签订合同
            $scope.contractForm.msg = null;
            if ($scope.contractForm.starToDemandProvider < 1 || $scope.contractForm.starToSolutionProcessProvider < 1) {
                $scope.contractForm.msg = "请评分！";
                return;
            }
            if (!$scope.contractForm.contract) {
                $scope.contractForm.msg = "请上传合同！";
                return;
            }
            if (isNaN($scope.contractForm.bankAccount)) {
                $scope.contractForm.msg = "请再次确认您的银行卡账户！";
                return;
            }
            $scope.contractForm.signAContractAt = new Date();
            resource.process.demandProcessUpdate({
                id: solutionProcess.demandSideProcess.id,
                _embed: 'SolutionProcess'
            }, $scope.contractForm, function (data) {
                console.log('签订合同a:', solutionProcess.state);
                solutionProcess.demandSideProcess.contract = data.contract;
                solutionProcess.demandSideProcess.signAContractAt = data.signAContractAt;
                solutionProcess.demandSideProcess.bankAccount = data.bankAccount;
                solutionProcess.demandSideProcess.bank = data.bank;
                solutionProcess.demandSideProcess.starToDemandProvider = data.starToDemandProvider;
                solutionProcess.demandSideProcess.starToSolutionProcessProvider = data.starToSolutionProcessProvider;
                solutionProcess.state = data.solutionProcess.state;
                // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                $scope.addRemark('合作达成，准备签约', solutionProcess);
                console.log('签订合同b:', solutionProcess.state);
            }, function (err) {
                MetaRemind.error("出错啦", err)
            });
        };

        /**
         * @function 添加备注
         */
        $scope.addRemark = function (content, solutionProcess) {
            console.log(solutionProcess);
            $scope.remarkForm.tag = Object.keys($scope.processStates)[solutionProcess.state];
            $scope.remarkForm.content = content ? content : $scope.remarkForm.content;
            $scope.remarkForm.solutionProcessId = solutionProcess.id;
            resource.note.add({
                userId: '_self'
            }, $scope.remarkForm, function (data) {
                //MetaRemind.success("添加成功！");
                getRemark(solutionProcess);
                $scope.remarkForm.content = '';
            }, function (err) {
                //MetaRemind.error("添加失败！");
            });
        };
        ///**
        // * @function 删除备注
        // * @param index 索引
        // */
        //$scope.deleteRemark = function (remarkId, solutionProcessId) {
        //    resource.note.delete({
        //        id: remarkId
        //    }, function (data) {
        //        MetaRemind.success("删除成功！");
        //        getRemark(solutionProcessId);
        //    }, function (err) {
        //        MetaRemind.error("删除失败！");
        //    });
        //};
        /**
         * @function 查看进程的备注
         * @param solutionProcessId 进程id
         */
        var getRemark = function (solutionProcess) {
            console.log('2', solutionProcess);
            resource.note.get({
                userId: '_self',
                solutionProcessId: solutionProcess.id,
                _sort: '-updatedAt'
            }, function (data) {
                solutionProcess.remarks = data;
            }, function (err) {
                MetaRemind.error("出错啦！", err);
            });
        };

        /**
         * @function 拒绝方案
         */
        $scope.reject = function (solutionProcess) {
            var modalInstance = $uibModal.open({
                animation: false,
                templateUrl: 'views/project/common/projectRejectModal.html',
                controller: ['$scope', '$uibModalInstance', function ($scope, $uibModalInstance) {
                    $scope.reason = '';
                    console.log($scope);
                    $scope.cancel = function () {
                        $uibModalInstance.dismiss('cancel');
                    };
                    $scope.ok = function () {
                        $uibModalInstance.close($scope.reason);
                    };
                }],
                size: 'md'
            });
            modalInstance.result.then(function (str) {
                console.log('before reject', str);
                console.log("before reject state:", solutionProcess.state);
                resource.process.demandProcessUpdate({
                    id: solutionProcess.demandSideProcess.id,
                    _embed: 'SolutionProcess'
                }, {
                    causeForCancel: str
                }, function (data) {
                    console.log("rejectdata:", data);
                    // solutionProcess.state = $scope.$parent.getStateNumber(data.solutionProcess.state);
                    // solutionProcess.state = data.solutionProcess.state;
                    solutionProcess.canceled = data.solutionProcess.canceled;
                    console.log("after reject state:", solutionProcess.state);
                }, function (err) {
                    MetaRemind.error("出错啦", err)
                });
            }, function () {
            });
        };

        /**
         * @function 始终有一个打开
         * @param $event 点击事件
         * @param scope 当前作用域
         */
        $scope.metaToggleOpen = function ($event, scope) {
            if (!$event.target.parentNode.parentNode.parentNode.parentNode.parentNode.classList.contains('panel-open')) {
                scope.toggleOpen();
            }
        };

        $scope.page.data = $scope.$parent.datas.charge;
    }]);
